#!/bin/zsh
export NOW_TASK_DI="/tmp/tasks"
export NOW_TASK_ITEM_FOCUS="/tmp/tasks-focus"
ICON="✔"
NO_ICON="x"

if [ ! -e $NOW_TASK_DI ];then
  echo "/tmp/test" > $NOW_TASK_DI;
fi

if [ ! -e $NOW_TASK_ITEM_FOCUS ];then
  echo "" > $NOW_TASK_ITEM_FOCUS;
fi

task() {
  if [ -e $1 ];then
    echo "$1" > $NOW_TASK_DI;
    export NOW_TASK="$1";
  fi
}

get_full_item_name_by_key(){
  local name=$1
  NOW_TASK="$(sed -n '1p' $NOW_TASK_DI)"
  cat << EOF | python3
with open("$NOW_TASK") as fp:
    for l in fp:
        if not l.strip().startswith("-"):continue
        l = l.strip()
        if "$name" in l:
            if l.endswith(']'):
                print("$NOW_TASK" + l[1:-4].replace(' ','-'))
            else:
                print("$NOW_TASK" + l[1:].replace(' ','-'))
            break
EOF
}

todo(){
  fname="$(get_full_item_name_by_key $1 )"
  if [[ $fname == "" ]];then
      echo "Not found in this task"
      exit 1
  fi
  

  if [ $# -eq 2 ];then
    if [[ $1 == "-i" ]];then
      check $2
      task_item $2;
    else
      check $1
      echo "$2"  | tee $fname
    fi
  elif [ $# -eq 1 ];then
    
    check $1
    while read data; do
      echo "$data"  | tee $fname
    done

  else
    echo "not enough!"
    :
  fi
}

_focus() {
  local focus="$(sed -n '1p' $NOW_TASK_ITEM_FOCUS )"
  echo $focus
}


check() {
  NOW_TASK="$(sed -n '1p' $NOW_TASK_DI)"
  key=$1
  cat << EOF | python3
d=[]
with open("$NOW_TASK") as fp:
    for l in fp:
        l = l.strip()
        if not l:continue
        if "$key" in l and "[C]" in l:
            l = l[:-3]
        elif "$key" in l and not l.endswith("[C]"):
            if '[T]' in l:
                l = l[:-3] + "[C]"
            else:
                l = l + "[C]"
        d.append(l)
with open("$NOW_TASK", "w") as fp:
    fp.write("\n".join(d))
EOF
}
finish() {
  NOW_TASK="$(sed -n '1p' $NOW_TASK_DI)"
  key=$1
  cat << EOF | python3
d=[]
with open("$NOW_TASK") as fp:
    for l in fp:
        l = l.strip()
        if not l:continue
        if "$key" in l and "[T]" in l:
            l = l[:-3]
        elif "$key" in l and not l.endswith("[T]"):
            if '[C]' in l:
                l = l[:-3] + "[T]"
            else:
                l = l + "[T]"
        d.append(l)
with open("$NOW_TASK", "w") as fp:
    fp.write("\n".join(d))
EOF
}

finish_task() {
  NOW_TASK="$(sed -n '1p' $NOW_TASK_DI)"
  key=$1
  sed -i "s/$key/& [T]/" $NOW_TASK;
}


task_item() {
  fname="$(get_full_item_name_by_key $1 )"
  if [[ $fname == "" ]];then
      echo "Not found in this task"
      exit 1
  fi
  tmux split-window -h -l 50 "vim $fname"
}


create_task() {
  fname="/tmp/$1"
  if [[ $fname == "/tmp/" ]];then
      echo "Not found in this task"
      exit 1
  fi
  tmux split-window -h -l 50 "vim $fname"
  task $fname
}

todo_update(){
  git clone https://gitee.com/dark.H/tmux-todo-plugin $HOME/tmux-plugins/todo-plugin.bak;
  mv $HOME/tmux-plugins/todo-plugin.bak $HOME/tmux-plugins/todo-plugin
  cp $HOME/tmux-plugins/todo-plugin/scripts/todo_plugin.sh $HOME/;
  tmux source-file $HOME/.tmux.conf;
}